import 'package:flutter/material.dart';
import "./wiget/KeepAlive.dart";

void main() {
  runApp(const MaterialApp(home: HomePage()));
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('你好flutter1')),
        body: PageView.builder(
          itemCount: 5,
          itemBuilder: (context, index) {
            // TODO 缓存组件包裹
            return MyKeepAlive(
              child: MyContainer(num: index),
            );
          },
        ),
      ),
    );
  }
}

class MyContainer extends StatefulWidget {
  int num;
  MyContainer({super.key, required this.num});

  @override
  State<MyContainer> createState() => _MyContainerState();
}

class _MyContainerState extends State<MyContainer>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    print(widget.num);
    return Center(
      child: Text('${widget.num}'),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
